package com.blb.mapper;

import com.blb.entity.Company;
import com.blb.entity.Order;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.blb.entity.Product;
import com.blb.entity.ProductionLine;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author HuaHao
 * @since 2019-11-15
 */
public interface OrderMapper extends BaseMapper<Order> {
    /**
     * 模糊查询,分页
     * @param orderId
     * @param dealTime
     * @return
     */
 List<Map<String,Object>> findAllOrderList(@Param("supplierId") String supplierId,@Param("start") int start, @Param("length") int length,@Param("orderId") String orderId, @Param("dealTime") String dealTime,@Param("orderStatus") String orderStatus);

    int count(@Param("supplierId") String supplierId,@Param("orderId") String orderId, @Param("dealTime") String dealTime,@Param("orderStatus") String orderStatus);

    /**
     * 模糊查询，和显示列表
     * @param orderId
     * @param dealTime
     * @return
     */
 public List<Map<String,Object>> getAll(@Param("orderId") String orderId, @Param("dealTime") Date dealTime);

    /**
     * 添加中的下拉列表数据
     * @return
     */
 @Select("SELECT p_name,id from t_product where company_id =#{companyId}")
 public  List<Product> findAllProduct(String companyId);


 @Select("SELECT\tid,company_name FROM s_company  WHERE id IN (SELECT  r.customer_id FROM s_company  c LEFT JOIN t_relation r ON c.id=r.supplier_id WHERE c.id=#{companyId} )")
 public  List<Company> findAllCustom(String companyId);


    /**
     * 添加功能
     * @param order
     */
 @Insert("insert into t_order (id,order_status,order_id,product_id,custom_id,money,order_type,number,operator,pay_type,deal_time,supplier_id,order_holder) values (#{id},#{orderStatus},#{orderId},#{productId},#{customId},#{money},#{orderType},#{number},#{operator},#{payType},#{dealTime},#{supplierId},#{orderHolder})")
 void addOrder(Order order);

    /**
     * 判断发货与否
     * @param order
     */
 @Update("update  t_order set order_status=#{orderStatus},explains=#{explains},product_status=#{productStatus} where order_id=#{orderId}")
 void updateProduce( Order order);

 @Update("update  t_order set order_status=#{orderStatus},explains=#{explains} where order_id=#{orderId}")
 void updateGoods(Order order);

    /**
     * 查询一条数据，给列表页面详细信息用
     * @param orderId
     * @return
     */
 @Select(" SELECT o.*,c.*,p.*,tl.* FROM t_order o\n" +
         "        LEFT JOIN s_company c ON o.custom_id=c.id\n" +
         "        LEFT JOIN t_product p ON o.product_id=p.id \n" +
         "        LEFT JOIN  t_logistics tl ON o.shipping_method_id=tl.logistics_id  WHERE order_id=#{orderId} and supplier_id=#{supplierId}")
Map<String,Object> findOrderByOrderID(@Param("supplierId") String supplierId,@Param("orderId") String orderId);



    List<Map<String,Object>> findAllSort(@Param("supplierId") String supplierId,@Param("start") int start,@Param("length") int length,@Param("orderId") String orderId);

    int countSort(@Param("supplierId") String supplierId,@Param("orderId") String orderId);


/**
 * 查找生产线
 */
@Select("select id,production_name from t_production_line where company_id =#{companyId}")
public List<ProductionLine> findAllProductionLine(String companyId);

/**
 * 查找数量和生产能力，用于算出结束日期
 */
@Select("SELECT o.number,pl.production_capacity FROM t_order o LEFT JOIN t_production_line pl ON o.product_line_id = pl.id WHERE order_id =#{orderId} AND product_line_id=#{productLineId}")
Order findNumberAndProduction(String orderId,String productLineId);

/**
 * 设计排单
 */
@Update("UPDATE  t_order SET product_status=#{productStatus},product_plan_no=#{productPlanNo},product_plan_time=#{productPlanTime},product_plan_overtime=#{productPlanOvertime} where order_id=#{orderId}")
void updateProductSort(Order order1);

    /**
     * 为了设计排单专门写的先修改生产线，然后在查询
     * @param orderId
     */
    @Update("update t_order set product_line_id=#{productLineId} where order_id=#{orderId}")
    void updateOne(String orderId, String productLineId);

    /**
     * lifeng
     * 待生产订单列表显示 （订单表）
     *
     * @return
     */
    List<Map<String,Object>> PendingOrder(@Param("start") int start, @Param("length") int length, @Param("orderId") String orderId,@Param("companyId") String companyId);

    /**
     * lifeng
     * 计算数据总数
     * @return
     */
    int count2(@Param("orderId") String orderI,@Param("companyId") String companyId);

    /**
     * 查生产编号是否重复
     * @param companyId
     * @return
     */
    @Select("SELECT product_plan_no FROM t_order WHERE supplier_id=#{companyId}")
    List<String> findProductById(String companyId);

    /**
     * 消息提醒总数
     * @return
     */
    @Select("SELECT count(o.product_id) FROM t_order o " +
            "LEFT JOIN t_product p ON o.product_id=p.p_number " +
            "LEFT JOIN t_production_line l ON o.product_line_id=l.id WHERE o.product_status=\"6\"")
    int remindNum();

    /**
     * 订单详情中看是否有货
     * @return
     */
    @Select("SELECT o.*,p.*,(CASE WHEN `status` IS NULL THEN '无货' ELSE '有货' END) `status`,COUNT(*) FROM t_order o LEFT JOIN t_product p ON o.product_id=p.id LEFT JOIN t_cargo tc ON p.p_number=tc.product_no WHERE o.order_id=#{orderId} AND supplier_id=#{supplierId}")
 Map<String,Object> findstatus(@Param("orderId") String orderId,@Param("supplierId") String supplierId);
    /**
     * lifeng
     * 加到打印激光列表
     * @param orderId
     * @param productTime
     * @param printer
     * @param productLineId
     */
    @Update("update t_order set product_line_id=#{productLineId},printer=#{printer},product_time=#{productTime}" +
            " where order_id=#{orderId} and supplier_id=#{companyId}")
    void updatePendingOrder(String orderId,String productTime,String printer,int productLineId,String companyId);

    /**
     * 将激光码状态从null变为-1
     * @param orderId
     * @param companyId
     */
    @Update("update t_order set laser_code='-1' where order_id=#{orderId} and supplier_id=#{companyId}")
    void updateLStats(String orderId,String companyId);
    /**
     * 采购列表签收修改订单状态为已完成
     * xuyuanxin
     *
     * @param orderId
     */
    @Update("update t_order set order_status='4' where order_id=#{orderId}")
    void updateStatus(String orderId);

    /**
     采购列表签收修改车辆状态为无任务
     * xuyuanxin
     * @param orderId
     */
    void updateVehicle(String orderId);
    /**
     采购列表签收修改司机状态为无任务
     * xuyuanxin
     * @param orderId
     */
    void updateDriver(String orderId);
}
